package caronas.teste;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import caronas.modelo.Carona;

public class HqlTeste {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String queryString = ("from Carona");
		/*
				"select u.nome, u.email, p.descricao, p.latitude, p.longitude, q.descricao, q.latitude, q.longitude " +
				"from Usuario u, Carona c, Ponto p, Ponto q " +
				"where c.origem_id = p.id and c.destino_id = q.id";
		//*/
		//*
		AnnotationConfiguration configuration = new AnnotationConfiguration();
		configuration.configure();
		SessionFactory factory = configuration.buildSessionFactory();
		Session session = factory.openSession();
		Query query = session.createQuery(queryString);
		List<Carona> listaDeCaronas = query.list();
		for (Carona carona: listaDeCaronas) {
			System.out.println(carona);
		}
		//*/
		
		/*
		try {
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/caronas", "root", "123");
			PreparedStatement stmt = connection.prepareStatement(queryString);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {				
				String linha = new String();
				
				linha += rs.getString("u.nome");
				linha += rs.getString("u.email");
				
				linha += rs.getString("p.descricao");
				linha += rs.getDouble("p.latitude");
				linha += rs.getDouble("p.longitude");
		
				linha += rs.getString("q.descricao");
				linha += rs.getDouble("q.latitude");
				linha += rs.getDouble("q.longitude");
				
				System.out.println(linha);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		//*/
	}
}
